package com.seguriapp.gwt.client.mvp;

import javax.inject.Singleton;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.seguriapp.administration.gwt.client.EditUserDataPage;
import com.seguriapp.administration.gwt.client.UsersListPage;
import com.seguriapp.administration.gwt.client.activity.EditUserDataActivity;
import com.seguriapp.administration.gwt.client.activity.UsersListActivity;
import com.seguriapp.administration.gwt.client.place.EditUserDataPlace;
import com.seguriapp.administration.gwt.client.place.UserListsPlace;
import com.seguriapp.alerts.gwt.client.AlertDetailsPage;
import com.seguriapp.alerts.gwt.client.AlertsListPage;
import com.seguriapp.alerts.gwt.client.activity.AlertDetailsActivity;
import com.seguriapp.alerts.gwt.client.activity.AlertsListActivity;
import com.seguriapp.alerts.gwt.client.place.AlertDetailsPlace;
import com.seguriapp.alerts.gwt.client.place.AlertsListPlace;
import com.seguriapp.common.gwt.client.AbstractSeguriAppPlace;
import com.seguriapp.common.gwt.client.SeguriAppClientSession;
import com.seguriapp.complaints.gwt.client.ComplaintDetailsPage;
import com.seguriapp.complaints.gwt.client.ComplaintsListPage;
import com.seguriapp.complaints.gwt.client.RegisterComplaintPage;
import com.seguriapp.complaints.gwt.client.activity.ComplaintDetailsActivity;
import com.seguriapp.complaints.gwt.client.activity.ComplaintsListActivity;
import com.seguriapp.complaints.gwt.client.activity.RegisterComplaintActivity;
import com.seguriapp.complaints.gwt.client.place.ComplaintDetailsPlace;
import com.seguriapp.complaints.gwt.client.place.ComplaintsListPlace;
import com.seguriapp.complaints.gwt.client.place.RegisterComplaintPlace;
import com.seguriapp.crimeactivity.gwt.client.CrimeActivityPage;
import com.seguriapp.crimeactivity.gwt.client.activity.CrimeActivityActivity;
import com.seguriapp.crimeactivity.gwt.client.place.CrimeActivityPlace;
import com.seguriapp.entitieslist.gwt.client.EntitiesListPage;
import com.seguriapp.entitieslist.gwt.client.activity.EntitiesListActivity;
import com.seguriapp.entitieslist.gwt.client.place.EntitiesListPlace;
import com.seguriapp.gwt.client.PrincipalPage;
import com.seguriapp.paperworks.gwt.client.PaperworksListPage;
import com.seguriapp.paperworks.gwt.client.activity.PaperworksListActivity;
import com.seguriapp.paperworks.gwt.client.place.PaperworksListPlace;

/**
 * Mapper of activities to places.
 */
@Singleton
public class SeguriAppActivityMapper implements ActivityMapper {

  private final DispatchAsync dispatchAsync;
  private final EventBus eventBus;
  private final PlaceController placeController;

  private final PrincipalPage principalPage;
  private final AlertsListPage alertListPage;
  private final ComplaintsListPage complaintListPage;
  private final AlertDetailsPage alertDetailsPage;
  private final ComplaintDetailsPage complaintDetailsPage;
  private final CrimeActivityPage crimeActivityPage;
  private final UsersListPage usersListPage;
  private final EditUserDataPage editUserDataPage;
  private final RegisterComplaintPage registerComplaintPage;
  private final EntitiesListPage entitiesListPage;
  private final PaperworksListPage paperworksListPage;

  @Inject
  public SeguriAppActivityMapper(final DispatchAsync dispatchAsync, final EventBus eventBus, final PlaceController placeController,
      final PrincipalPage principalPage, AlertsListPage alertListPage, ComplaintsListPage complaintListPage, AlertDetailsPage alertDetailsPage,
      ComplaintDetailsPage complaintDetailsPage, CrimeActivityPage crimeActivityPage, final UsersListPage usersListPage,
      final EditUserDataPage editUserDataPage, final RegisterComplaintPage registerComplaintPage, final EntitiesListPage entitiesListPage,
      final PaperworksListPage paperworksListPage) {
    this.dispatchAsync = dispatchAsync;
    this.eventBus = eventBus;
    this.placeController = placeController;
    this.principalPage = principalPage;
    this.alertListPage = alertListPage;
    this.complaintListPage = complaintListPage;
    this.alertDetailsPage = alertDetailsPage;
    this.complaintDetailsPage = complaintDetailsPage;
    this.crimeActivityPage = crimeActivityPage;
    this.usersListPage = usersListPage;
    this.editUserDataPage = editUserDataPage;
    this.registerComplaintPage = registerComplaintPage;
    this.entitiesListPage = entitiesListPage;
    this.paperworksListPage = paperworksListPage;
  }

  @SuppressWarnings({ "unused" })
  public Activity getActivity(Place place) {
    Activity resultActivity = null;
    final Widget panelCentral = principalPage.getPanelCentralAsWidget();
    if (!(place instanceof AbstractSeguriAppPlace)) {
      return null;
    }
    final SeguriAppClientSession clientSession = SeguriAppClientSession.get();
    // TODO validate user permissions
    // final Place newPlace = validatePermissions(clientSession,
    // ((AbstractSeguriAppPlace) place).getIdMenu());
    // place = newPlace != null ? newPlace : place;
    final AbstractSeguriAppPlace seguriappPlace = (AbstractSeguriAppPlace) place;

    if (place instanceof AlertsListPlace) {
      resultActivity = new AlertsListActivity(dispatchAsync, eventBus, placeController, alertListPage, seguriappPlace, clientSession);
    } else if (place instanceof ComplaintsListPlace) {
      resultActivity = new ComplaintsListActivity(dispatchAsync, eventBus, placeController, complaintListPage, seguriappPlace, clientSession);
    } else if (place instanceof AlertDetailsPlace) {
      resultActivity = new AlertDetailsActivity(dispatchAsync, eventBus, placeController, alertDetailsPage, seguriappPlace, clientSession);
    } else if (place instanceof ComplaintDetailsPlace) {
      resultActivity = new ComplaintDetailsActivity(dispatchAsync, eventBus, placeController, complaintDetailsPage, seguriappPlace, clientSession);
    } else if (place instanceof CrimeActivityPlace) {
      resultActivity = new CrimeActivityActivity(dispatchAsync, eventBus, placeController, crimeActivityPage, seguriappPlace, clientSession);
    } else if (place instanceof UserListsPlace) {
      resultActivity = new UsersListActivity(dispatchAsync, eventBus, placeController, usersListPage, seguriappPlace, clientSession);
    } else if (place instanceof EditUserDataPlace) {
      resultActivity = new EditUserDataActivity(dispatchAsync, eventBus, placeController, editUserDataPage, seguriappPlace, clientSession);
    } else if (place instanceof RegisterComplaintPlace) {
      resultActivity = new RegisterComplaintActivity(dispatchAsync, eventBus, placeController, registerComplaintPage, seguriappPlace, clientSession);
    } else if (place instanceof EntitiesListPlace) {
      resultActivity = new EntitiesListActivity(dispatchAsync, eventBus, placeController, entitiesListPage, seguriappPlace, clientSession);
    } else if (place instanceof PaperworksListPlace) {
      resultActivity = new PaperworksListActivity(dispatchAsync, eventBus, placeController, paperworksListPage, seguriappPlace, clientSession);
    }
    return resultActivity;
  }

  // TODO validatePermissions
//  @SuppressWarnings("unchecked")
//  private Place validatePermissions(final SeguriAppClientSession clientSession, final String menuId) {
//    Place place = null;
//    boolean validMenu = false;
//    final User user = (User) clientSession.get(Key.User);
//    List<MenuLink> menuLinks = null;
//    if (user != null) {
//      System.out.println(user.getUsername());
//      // TODO
//    } else {
//      menuLinks = (List<MenuLink>) clientSession.get(Key.NoUserMenus);
//    }
//    for (final MenuLink menuLink : menuLinks) {
//      if (menuId.equals(menuLink.getPath())) {
//        validMenu = true;
//        break;
//      }
//    }
//    if (!validMenu) {
//      if (user != null) {
//        place = new ComplaintsListPlace(String.valueOf(new Date().getTime()));
//      } else {
//        place = new CrimeActivityPlace(String.valueOf(new Date().getTime()));
//      }
//    }
//    return place;
//  }

}
